﻿using System.Collections.Generic;

namespace QSearcher.QCrawler
{
    public class BizLayer
    {
        private static object _lock = new object();
        private static BizLayer self = null;
        public static BizLayer Self
        {
            get
            {
                if (self == null)
                    lock (_lock)
                        if (self == null) self = new BizLayer();
                return self;
            }
        }

        private CrawlerServer.CrawlerServicer client = new CrawlerServer.CrawlerServicer();

        public void SendStartUrl(string analyzerName, string url)
        {
            client.SetStartUrl(analyzerName, url);
        }

        public class RequestUrl
        {
            public int Code { set; get; }
            public string Msg { set; get; }
            public string ID { set; get; }
            public string Url { set; get; }
        }

        public RequestUrl GetRequestUrl()
        {
            var res = client.GetRequestUrl();
            return new RequestUrl()
            {
                Code = res.Code,
                Msg = res.Msg,
                ID = res.ID,
                Url = res.Url,
            };
        }

        public class RequestResult
        {
            public string Msg { set; get; }
            public string ID { set; get; }
        }

        public void SendRequestResult(RequestResult result)
        {
            client.SendRequestResult(result.ID, result.Msg);
        }

        public class DownloadUrl
        {
            public int Code { set; get; }
            public string Msg { set; get; }
            public string ID { set; get; }
            public string Url { set; get; }
        }

        public DownloadUrl GetDownloadUrl()
        {
            var res = client.GetDownloadUrl();
            return new DownloadUrl()
            {
                Code = res.Code,
                Msg = res.Msg,
                ID = res.ID,
                Url = res.Url,
            };
        }

        public class DownloadResult
        {
            public string Msg { set; get; }
            public string ID { set; get; }
        }

        public void SendDownloadResult(DownloadResult result)
        {
            client.SendDownloadResult(result.ID, result.Msg);
        }

        public class AnalyzeSource
        {
            public int Code { set; get; }
            public string Msg { set; get; }
            public string ID { set; get; }
            public string Url { set; get; }
        }

        public AnalyzeSource GetAnalyzeSource(string analyzerName)
        {
            var res = client.GetAnalyzeSource(analyzerName);
            return new AnalyzeSource()
            {
                Code = res.Code,
                Msg = res.Msg,
                ID = res.ID,
                Url=res.Url,
            };
        }

        public class AnalyzeResult
        {
            public string ID { set; get; }
            public string Msg { set; get; }
            public List<string>  RequestUrls { set; get; }
            public List<string> DownloadUrls { set; get; }
        }

        public void SendAnalyzeResult(AnalyzeResult result)
        {
            client.SendAnalyzeResult(result.ID, result.RequestUrls.ToArray(), result.DownloadUrls.ToArray(), result.Msg);
        }
    }
}
